#include "mylabel.h"



MyLabel::MyLabel(const bool b, const QString &msg, QWidget *parent)
    : QLabel(parent)
    , colour(b)
    , _msg(msg)
{
    QFont font("华文细黑", 12);
    font.setWeight(QFont::Light); // 设置为轻量级，这通常类似于细体
    this->setFont(font);

    setMargin(10);
    setAlignment(Qt::AlignJustify);//对齐模式
    setWordWrap(true);//文本自动换行
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);//扩展模式
    setAutoFillBackground(true);
    setFont(this->font());
    setTextInteractionFlags(Qt::TextSelectableByMouse);
    setText(_msg);
    adjustSize();//自动调整大小


    if (colour){
        setStyleSheet(qss.arg("149,236,105"));
    }else{
        setStyleSheet(qss.arg("255,255,255"));
    }
}

MyLabel::~MyLabel()
{}

QSize MyLabel::sizeHint() const
{
    static QRect textRect;
    QFontMetrics fm(this->font());
    textRect = fm.boundingRect(45, 20, width(), INT_MAX, Qt::TextWordWrap, _msg);
    return QSize(textRect.width() + 50, textRect.height() + 50);
}

void MyLabel::enterEvent(QEnterEvent *)
{
    // 进入区域，改变光标形状
    setCursor(Qt::IBeamCursor);
    if (colour){
        setStyleSheet(qss.arg("137,217, 97"));
    }else{
        setStyleSheet(qss.arg("235,235,235"));
    }
}

void MyLabel::leaveEvent(QEvent *)
{
    // 离开区域，恢复默认光标
    unsetCursor();
    setSty();
}


